<template>
  <view class="page page-primary page-tenant">
    <page-header theme="colorful"></page-header>
    <view class="page-content">
      <view class="user-wrapper">
        <image class="user-avatar" :src="userInfo.avatar || '/static/images/company/person-head.png'"></image>
        <view class="user-text">
          <fui-text :text="userInfo.nickname" fontWeight="bold" color="#fff"></fui-text>
          <fui-text :text="userInfo.mobile" :padding="['0', '10rpx']" size="28" color="#fff"></fui-text>
        </view>
      </view>
      <view class="tenant-list">
        <view class="tenant-item" v-for="item in tenantList" :key="item.id" @click="handleChange(item)">
          <view class="tenant-item-current" v-if="item.id === currentTenantId">
            <view class="circle-badge"></view>
            <fui-text text="当前使用" size="20"></fui-text>
          </view>
          <image class="tenant-item-icon" src="/static/images/icon.png"></image>
          <view class="tenant-item-name">{{ item.title }}</view>
        </view>
      </view>
      <view class="tenant-tips">
        <fui-text text="轻触列表项以切换供应链" size="28" color="#999"></fui-text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, inject } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { useStore } from 'vuex'
import pageHeader from '@/components/header'

const store = useStore()
const tab = inject('$tab')
const modal = inject('$modal')

const { userInfo, tenantList, currentTenantId } = store.getters

const handleChange = async (tenant) => {
  if (tenant.id === store.getters.currentTenantId) return
  modal.loading()
  try {
    // 退出当前用户
    await store.dispatch('LogoutTenant')

    // 切换租户
    await store.dispatch('SetTenantId', tenant.id)

    // 租户静默登录
    await store.dispatch('LoginTenant', {
      username: store.getters.userInfo.username,
      password: '********',
      captchaVerification: '********',
      tenantName: tenant.name,
      rememberMe: false,
    })

    // 获取部门信息
    await store.dispatch('GetDeptList')

    // 设置用户信息
    const res = await store.dispatch('GetUserInfo')
    modal.closeLoading()

    // 校验用户状态
    const userInfo = res.data
    const userArr = userInfo && userInfo.userType ? userInfo.userType.split(',') : []
    const userType = userArr.indexOf('2') > -1 ? '2' : '1'
    if (userType !== '1' && userType !== '2') {
      modal.msgError('无权限的用户！')
      store.dispatch('LogOut')
      return
    }
    if (userType === '1' && !userInfo.qyzt) {
      modal.msgError('供应链未启用！')
      store.dispatch('LogOut')
      return
    }

    // 切换成功，回到首页
    modal.msg('切换成功！')
    tab.reLaunch('/pages/index')
  } catch(err) {
    modal.closeLoading()
    if (err && err.msg) {
      modal.msg(err.msg)
    } else {
      modal.msg('切换失败，请重新登录！')
    }
    store.dispatch('LogOut')
  }
}
</script>

<style lang="scss" scoped>
.page-tenant {
  background-size: 100% 460rpx;
  ::v-deep .ui-header-colorful {
    background-size: 100% 460rpx !important;
  }
  .page-content {
    padding-top: 10rpx;
  }
  .user-wrapper {
    .user-avatar {
      display: block;
      width: 120rpx;
      height: 120rpx;
      border-radius: 50%;
      background-color: #fff;
      overflow: hidden;
      margin: 24rpx auto;
    }
    .user-text {
      text-align: center;
    }
  }
  .tenant-list {
    margin: 60rpx 0;
    .tenant-item {
      position: relative;
      display: flex;
      align-items: center;
      border-radius: 16rpx;
      background-color: #fff;
      margin: 36rpx 0;
      padding: 36rpx 24rpx;
      .tenant-item-current {
        position: absolute;
        top: 0;
        right: 0;
        margin: 0 24rpx;
        .circle-badge {
          display: inline-block;
          width: 12rpx;
          height: 12rpx;
          border-radius: 50%;
          background-color: #3aad51;
          margin-right: 5rpx;
        }
      }
      .tenant-item-icon {
        flex: 0 0 80rpx;
        width: 80rpx;
        height: 80rpx;
        margin-right: 24rpx;
      }
      .tenant-item-title {
        flex: 1;
      }
    }
  }
  .tenant-tips {
    text-align: center;
  }
}
</style>